package com.westa.smallshop.util;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.android.volley.AuthFailureError;
import com.android.volley.NoConnectionError;
import com.android.volley.Response;
import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;
import com.westa.smallshop.R;
import com.westa.smallshop.data.HttpError;

import java.io.UnsupportedEncodingException;

import timber.log.Timber;

/**
 * Created by dune on 15/12/23.
 */

public class HttpUtils {

    public final static String CHARACTER_ENCODING = "utf-8";
    //        public static String host = "http://192.168.31.193:8000/";
//    public static String host = "http://192.168.1.121:8000/";
    //    public static String host = "http://192.168.1.126/";
//    http://59.110.9.249:8080/shop/myself?account=111111
    public static String host = "http://59.110.9.249:8080/shop/";
//    public static String SHARE_HOST = "http://m.qducc.com/";
//    public static String LIBRARY_HOST = "http://lib.qducc.com/";
//    public static String SHARE_HOST = "http://192.168.1.126/";

    public static String getHost() {
        return host;
    }

    public static String setHost(String mHost) {

        return host = mHost;
    }
    public interface Listener<T> {

        void onResponse(T response, int count);
    }

    public static abstract class ErrorListener implements Response.ErrorListener {

        private HttpError mHttpErrorData;
        private Context mContext;


        public ErrorListener(Context context) {
            mContext = context;
        }

        @Override
        public void onErrorResponse(VolleyError error) {

            if (error instanceof NoConnectionError || error instanceof TimeoutError) {
                errorMessage(mHttpErrorData, R.string.http_error_not_connection_server);
            } else if (error instanceof ServerError) {

                if (error.networkResponse != null && error.networkResponse.statusCode == 400) {
                    try {
                        String msg = new String(error.networkResponse.data, CHARACTER_ENCODING);
                        mHttpErrorData = JSONObject.parseObject(msg, HttpError.class);
                        if (mHttpErrorData.isNotAuth()) {
                            errorMessage(mHttpErrorData, R.string.http_error_not_auth);
                        } else if (mHttpErrorData.isNotFound()) {
                            errorMessage(mHttpErrorData, R.string.http_error_not_found);
                        } else {
                            errorMessage(mHttpErrorData, 0);
                        }
                    } catch (UnsupportedEncodingException e) {
                        Timber.e(e.getMessage());
                        errorMessage(mHttpErrorData, R.string.http_error_unknown, 4004);
                    } catch (Exception e) {
                        errorMessage(mHttpErrorData, R.string.http_error_unknown, 4010);
                    }
                } else if (error.networkResponse != null && error.networkResponse.statusCode == 404) {
                    errorMessage(mHttpErrorData, R.string.http_error_not_auth);
                } else {
                    errorMessage(mHttpErrorData, R.string.http_error_server);
                }
            } else if (error instanceof AuthFailureError &&
                    error.networkResponse != null && error.networkResponse.statusCode == 401) {
                try {
                    String msg = new String(error.networkResponse.data, CHARACTER_ENCODING);
                    mHttpErrorData = JSONObject.parseObject(msg, HttpError.class);
                    if (mHttpErrorData.isNotAuthLogin()) {
                        errorMessage(mHttpErrorData, R.string.http_error_account_auth_failed);
//                        Global.logout(mContext);
                    } else {
                        errorMessage(mHttpErrorData, 0);
                    }
                } catch (UnsupportedEncodingException e) {
                    Timber.e(e.getMessage());
                    errorMessage(mHttpErrorData, R.string.http_error_unknown, 4014);
                }

            } else {
                errorMessage(mHttpErrorData, R.string.http_error_unknown, 4000);
            }

        }

        public abstract void onErrorData(@Nullable HttpError httpError);

        public void errorMessage(@Nullable HttpError httpError, @StringRes int errorStringId, Object... args) {

            HttpError mHttpError = httpError;
            if (mHttpError == null) {
                mHttpError = new HttpError();
            }
            if (errorStringId != 0) {
                mHttpError.setContent(mContext.getString(errorStringId, args));
            } else {
                if (TextUtils.isEmpty(mHttpError.getContent())) {
                    mHttpError.setContent(mContext.getString(R.string.http_error_unknown, "5000"));
                }
            }
            onErrorData(mHttpError);
        }

    }
}
